<?php
/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of MoneyFactoryclass
 *
 * @author marius
 * Edited by jamal dine YOULHAJEN
 */
require_once("Money.class.php");
require_once ("MoneyExceptions.php");

class MoneyFactory {
    //put your code here

    private static $_defaultCurrencies;

    private static $_currencies;
	
	private $_curr;

    private static $_defaultInstance;

    public static function getDefaultFactory() {
	if (!isset($_defaultInstance))
		MoneyFactory::$_defaultInstance=new MoneyFactory();
	return MoneyFactory::$_defaultInstance;
    }
	
	public function __construct(){
        $this->curr=array("€","$","£","CHF");
    }
	/*
	public function MoneyFactory(){
		$this::$_currencies=array("€","$","£","CHF");
	}*/
	
	public static function getDefaultCurrencies() {
	if (!isset($_currencies))
		MoneyFactory::$_currencies=array("€","$","£","CHF");
	return MoneyFactory::$_currencies;
    }
	
	public function getCurrencies(){
		return $this->curr;
	}
	
	public function addCurrencie($curren) {
		if(in_array(strtoupper($curren), $this->curr)){
			throw new ExistingCurrencyException("MoneyFactory::ExistingCurrencyException");
		}
		$this->curr[]=strtoupper($curren);
	
	}
	
    public function createMoney($v,$curr) 
	{
		//var_dump($curr);
		//var_dump($this->curr);
		if(!$this->isValidCurrencyNS($curr))
		{
			throw new InvalidCurrencyException("MoneyFactory::createMoney");
		}
		if (!$this::isValidMoneyValue($v)) 
		{
			throw new InvalidMoneyValueException("MoneyFactory::createMoney");
		}
	   return new Money($v,$curr);
    }
	
	/**
	** Checks if a currency is valid
	**/
	public function isValidCurrency($c)
	{
		//var_dump(in_array($c, $this::getDefaultCurrencies()));
		//var_dump($this::getDefaultCurrencies());
      return in_array($c, $this::getDefaultCurrencies());
	}
	
	public function isValidCurrencyNS($c)
	{
		//var_dump(in_array($c, $this::getDefaultCurrencies()));
		//var_dump($this->curr);
      return in_array($c, $this->curr);
	}
	/**
	** Checks if a value is valid as money value
	**/
	private function isValidMoneyValue($v)
	{
	   if (is_numeric($v))
	   {
	     return true;
	   }
	   return false;
	}
}
?>
